<template>
  <XtxBread v-if="subcategory.top && subcategory.sub">
    <XtxBreadItem to="/">首页</XtxBreadItem>
    <XtxBreadItem :to="`/category/${subcategory.top.id}`">{{
      subcategory.top.name
    }}</XtxBreadItem>
    <Transition name="fade-right" mode="out-in">
      <XtxBreadItem
        :to="`/category/sub/${subcategory.sub.id}`"
        :key="subcategory.sub.id"
        >{{ subcategory.sub.name }}</XtxBreadItem
      >
    </Transition>
  </XtxBread>
</template>

<script>
import { computed } from 'vue'
import { useRoute } from 'vue-router'
import { useStore } from 'vuex'
export default {
  name: 'SubBread',
  setup () {
    const route = useRoute()
    const store = useStore()
    const subcategory = computed(() => {
      const cate = {}
      store.state.category.list.forEach((item) => {
        if (item.children) {
          const m = item.children.find((item1) => item1.id === route.params.id)
          if (m) {
            cate.top = { id: item.id, name: item.name }
            cate.sub = { id: m.id, name: m.name }
          }
        }
      })
      return cate
    })
    return { subcategory }
  }
}
</script>

<style></style>
